package com.sugon.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.PlanNeedCheckDao;
import com.sugon.entity.PlanNeedCheckEntity;
import com.sugon.entity.SysMsgEntity;
import com.sugon.service.PlanNeedCheckService;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysProcessInfoService;
import com.sugon.utils.ProcessInfoEnum;
import com.sugon.utils.ShiroUtils;
import com.sugon.utils.SysMsgType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 需求计划审批Service实现类
 *
 * @author sugon
 * @date 2019-08-13 10:50:10
 */
@Service("planNeedCheckService")
public class PlanNeedCheckServiceImpl implements PlanNeedCheckService {
    @Autowired
    private PlanNeedCheckDao planNeedCheckDao;
    @Autowired
    private SysProcessInfoService sysProcessInfoService;
    @Autowired
    private SysMsgService sysMsgService;

    /**
     * @param id 主键
     * @return
     */
    @Override
    public PlanNeedCheckEntity queryObject(String id) {
        return planNeedCheckDao.queryObject(id);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public List<PlanNeedCheckEntity> queryList(Map<String, Object> map) {
        return planNeedCheckDao.queryList(map);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return planNeedCheckDao.queryTotal(map);
    }

    /**
     * @param planNeedCheck 实体
     * @return
     */
    @Override
    public int save(PlanNeedCheckEntity planNeedCheck) {
        planNeedCheck.setId(RandomUtil.randomString(32));
        planNeedCheck.setCreateUser(ShiroUtils.getUserId());
        planNeedCheck.setCreateDate(new Date());
        planNeedCheck.setApplyUser(ShiroUtils.getUserId());
        planNeedCheck.setApplyDate(new Date());
        planNeedCheck.setDelFlag(1);
        planNeedCheck.setDeptCheckStatus(0);
        planNeedCheck.setMakeCheckStatus(0);
        planNeedCheck.setLeaderCheckStatus(0);
        sysProcessInfoService.saveProcessInfo(planNeedCheck.getId(), ProcessInfoEnum.POE_XQJH, 0, null);
        saveSysMsgEntity(planNeedCheck.getId(), 0);
        return planNeedCheckDao.save(planNeedCheck);
    }

    /**
     * @param planNeedCheck 实体
     * @return
     */
    @Override
    public int update(PlanNeedCheckEntity planNeedCheck) {
        planNeedCheck.setUpdateUser(ShiroUtils.getUserId());
        planNeedCheck.setUpdateDate(new Date());

        return planNeedCheckDao.update(planNeedCheck);
    }

    /**
     * @param planNeedCheck
     * @return
     */
    @Override
    public int deptCheck(PlanNeedCheckEntity planNeedCheck) {
        planNeedCheck.setDeptCheckUser(ShiroUtils.getUserId());
        planNeedCheck.setDeptCheckDate(new Date());
        sysProcessInfoService.saveProcessInfo(planNeedCheck.getId(), ProcessInfoEnum.POE_XQJH, planNeedCheck.getDeptCheckStatus(), null);
        saveSysMsgEntity(planNeedCheck.getId(), planNeedCheck.getDeptCheckStatus());
        return planNeedCheckDao.update(planNeedCheck);
    }

    /**
     * @param planNeedCheck
     * @return
     */
    @Override
    public int makeCheck(PlanNeedCheckEntity planNeedCheck) {
        planNeedCheck.setMakeCheckUser(ShiroUtils.getUserId());
        planNeedCheck.setMakeCheciDate(new Date());
        sysProcessInfoService.saveProcessInfo(planNeedCheck.getId(), ProcessInfoEnum.POE_XQJH, planNeedCheck.getMakeCheckStatus(), null);
        saveSysMsgEntity(planNeedCheck.getId(), planNeedCheck.getMakeCheckStatus());
        return planNeedCheckDao.update(planNeedCheck);
    }

    /**
     * @param planNeedCheck
     * @return
     */
    @Override
    public int leaderCheck(PlanNeedCheckEntity planNeedCheck) {
        planNeedCheck.setLeaderCheckUser(ShiroUtils.getUserId());
        planNeedCheck.setLeaderCheckDate(new Date());
        sysProcessInfoService.saveProcessInfo(planNeedCheck.getId(), ProcessInfoEnum.POE_XQJH, planNeedCheck.getLeaderCheckStatus(), null);
        saveSysMsgEntity(planNeedCheck.getId(), 2);
        return planNeedCheckDao.update(planNeedCheck);
    }

    /**
     * @param Id
     * @param status
     */
    public void saveSysMsgEntity(String Id, Integer status) {
        SysMsgEntity sysMsgEntity = new SysMsgEntity();

        if (2 == status) {
            //审核不通过，消息流程提醒结束
            sysMsgEntity.setDataId(Id);
            sysMsgEntity.setDataType(ProcessInfoEnum.POE_XQJH);
            sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
        } else {
            sysMsgEntity.setDataId(Id);
            sysMsgEntity.setDataUrl("../sys/planneedcheck.html");
            sysMsgEntity.setType(SysMsgType.APPROVAL_REMIND);
            sysMsgEntity.setUserId(ShiroUtils.getUserId());
            sysMsgEntity.setDept(ShiroUtils.getDeptId());
            sysMsgEntity.setTitle(ProcessInfoEnum.POE_XQJH.getDesc());
            sysMsgEntity.setDataType(ProcessInfoEnum.POE_XQJH);
        }

        sysMsgService.allMsgData(sysMsgEntity);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return planNeedCheckDao.delete(id);
    }

    /**
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return planNeedCheckDao.deleteBatch(ids);
    }
}
